const query = require('../config/db.config.js')
const {
  nowTime
} = require('../utils/formattime')

const hospitalMod = {
  cancleAppoiMod: async (data) => {
    const {
      uID
    } = data
    let sql =
      `
      DELETE
      FROM
        user_hsp_appo
      WHERE
       uID ="${uID}";
     `
    await query(sql)

    let sql2 =
      `UPDATE hospital 
   SET
   appointment_num = appointment_num - 1
   WHERE 
     hID = "1"`
    await query(sql2)
  },
  hspInfoMod: async () => {
    let sql =
      `SELECT  
      *
    FROM 
      hospital 
    `
    const res = await query(sql)
    return res
  },
  appoimentHspMod: async (data) => {
    const {
      uID,
      hID
    } = data
    let sql =
      `UPDATE hospital 
    SET
    appointment_num = appointment_num + 1
    WHERE 
      hID = "${hID}"`
    await query(sql)

    /// 查询存在
    let slectSql = `SELECT  
    *
    FROM 
      user_hsp_appo 
    WHERE 
    uID = "${uID}"
  `
    const time = nowTime()

    const isExsit = await query(slectSql)
    console.log(isExsit);
    if (isExsit.length !== 0) {
      //存在直接修改
      let sql =
        `UPDATE user_hsp_appo 
    SET
      ctime = "${nowTime()}",
    WHERE 
      uID = "${uID}"`
      await query(sql)
    } else {
      //不存在就创建
      let sql = `
    INSERT INTO user_hsp_appo ( hID , uID , ctime , status ) VALUES ( '1', "${uID}","${time}" , "1");
    `
      await query(sql)

    }
  },
  appoimentStatusMod: async (data) => {
    const {
      uID
    } = data
    let sql = `SELECT  
    *
    FROM 
      user_hsp_appo 
    WHERE 
    uID = "${uID}"
    `
    const res = await query(sql)
    return res
  },
  editeHosiptalWebMod:async (data)=>{
    const {hID , h_name, max_num , phone , img} = data
    let sql =
      `UPDATE hospital 
    SET
      h_name = "${h_name}",
      max_num = "${max_num}",
      phone = "${phone}",
      img = "${img}"
    WHERE 
      hID = "${hID}"`
      await query(sql)
  }
}

module.exports = hospitalMod